﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.IO;
using System.Configuration;
using System.Collections;

namespace Golf.WebUI
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            // DateTime.Now.ToOADate() 
            SkinSave(Server.MapPath("/Create_SC/" + txtKeyword.Text + ".cs"), GetStrCode());
        }

        public string GetStrCode()
        {
            string model = txtKeyword.Text.Trim();
            string ziduan = TextBox1.Text.Trim();
            string[] ZD = ziduan.Split(',');
            StringBuilder sb = new StringBuilder();
            sb.Append("\n context.Response.ContentType = \"text/plain\";");
            sb.Append("\n JavaScriptSerializer js = new JavaScriptSerializer();");
            sb.Append("\n Golf.BLL." + txtKeyword.Text + " bll = new Golf.BLL." + txtKeyword.Text + "();");
            sb.Append("\n List<Golf.Model." + txtKeyword.Text + "> list = new List<Golf.Model." + txtKeyword.Text + ">();");
            sb.Append("\n //实例化页面信息类，获取页面传来的参数。该类是FlexiGridHelper中定义的类，封装了很多功能需要的数据");
            sb.Append("\n PageView pageView = new PageView(context);");
            sb.Append("\n //获取分页后的数据集");
            sb.Append("\n  DataSet ds = bll.GetList(pageView.PageSize, pageView.PageIndex, \"\");");
            sb.Append("\n //构建表格");
            sb.Append("\n DataTable dt = new DataTable();");
            sb.Append("\n //构建列");
            sb.Append("\n  //注意：第一列必须是数据库的主键列");
            int i = 0;
            foreach (var item in ZD)
            {
                sb.Append("\n DataColumn dc" + i + " = new DataColumn(\"" + item + "\", typeof(string));");
                i++;
            }
            sb.Append("\n //把构建的列添加到数据表中");
            sb.Append("\n dt.Columns.AddRange(new DataColumn[] {");
            for (int j = 0; j < i; j++)
            {
                if (j != i - 1)
                    sb.Append("dc" + j + ",");
                else
                    sb.Append("dc" + j);
            }
            sb.Append(" });//这里有个顺序的哟 ");
            sb.Append("\n  list = bll.DataTableToList(ds.Tables[0]);//获取第一张表的数据  就是数据库中的数据");
            sb.Append("\n  //填充数据");

            sb.Append("\n foreach (var item in list)");
            sb.Append("\n {");

            sb.Append("\n  //新建行，注意是dt.NewRow();而不是new DataRow();");
            sb.Append("\n DataRow dr = dt.NewRow();");
            sb.Append("\n dt.Rows.Add(dr);");
            foreach (var item in ZD)
            {

                sb.Append("\n dr[\"" + item + "\"] = item." + item + ";");
            }
            sb.Append("\n }");
            sb.Append("\n  //调用FlexiGrid数据格式转换方法");
            sb.Append("\n context.Response.Write(js.Serialize(new GetDataHelper().GetFlexiGridData(pageView, dt, (int)ds.Tables[1].Rows[0][0])));//ds.Tables[1].Rows[0][0] 这里是数据库表的记录行数  是第二张表的 在dao类中的最后方法中");

            /* cells = new Array(
                new cell("用户id", "UserId", 50, true, "left", null),
                new cell("姓", "FristName", 100, true, "left", null),
                new cell("名", "LastName", 100, true, "left", null),
                new cell("性别", "sex", 80, true, "left", null),
                new cell("生日", "Birthday", 150, true, "left", null),
                new cell("手机", "Mobile", 30, true, "left", null),
                new cell("电话", "Tel", 120, true, "left", null),
                new cell("传真", "Fax", 120, true, "left", null),
                new cell("QQ", "QQ", 120, true, "left", null),
                new cell("Msn", "Msn", 80, true, "left", null),
                new cell("地址", "Address ", 150, true, "left", null),
                new cell("邮编", "PostCode", 30, true, "left", null),
                new cell("省", "Province", 120, true, "left", null),
                new cell("市", "City", 120, true, "left", null),
                new cell("区", "Borough", 120, true, "left", null),
                new cell("操作", "action", 120, true, "left", null)
            );*/
            sb.Append("\n /*");
            sb.Append("\n //下面的是页面代码");
            sb.Append("\n  cells = new Array(");
            int z = 0;
            foreach (var item in ZD)
            {
                if (z != i - 1)
                    sb.Append("\n   new cell(\"" + item + "\", \"" + item + "\", 100, true, \"left\", null),");
                else
                    sb.Append("\n   new cell(\"" + item + "\", \"" + item + "\", 100, true, \"left\", null)");
                z++;
            }
            sb.Append("\n );");
            sb.Append("\n */");
            return sb.ToString();
        }



        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="Path">保存路径</param>
        /// <param name="Save_Message">保存内容</param>
        private void SkinSave(string Url_Path, string Save_Message)
        {
            try
            {
                FileStream fs = null;
                //判断文件是否存在
                if (File.Exists(Url_Path))
                {
                    Response.Write("2|");
                }
                else
                {
                    //创建
                    fs = new FileStream(Url_Path, FileMode.Create);
                    byte[] arr = Encoding.Default.GetBytes(Save_Message);
                    fs.Write(arr, 0, arr.Length);
                    fs.Flush();
                    fs.Close();
                    Response.Write("0|");
                }
            }
            catch (Exception)
            {
                Response.Write("1|");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //if (Golf.WebUI.App_code.SentEmail.EmailPost("dufeng_Code@163.com", "DuFengLove1!6^3#", "343675209@qq.com,664320647@qq.com,1519444649@qq.com", true))

            if (Golf.WebUI.App_code.SentEmail.EmailPost("dufeng_Code@163.com", "DuFengLove1!6^3#", "343675209@qq.com", false))
            {
                Response.Write("发送成功");
            }
            else
            {
                Response.Write("发送失败");
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string mailSubject = "会员注册确认函";
            string mailBody = "正文内容。";
            //string mailFrom = ConfigurationManager.AppSettings["Emailname"];
            string mailFrom = "dufeng_Code@163.com";
            ArrayList List = new ArrayList();
            List.Add(Server.MapPath(@"'E:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\backup\a0717224423_59578975\1.bak"));
            if (new Golf.WebUI.App_code.SentEmail().MySendMail("343675209@qq.com", mailSubject, mailBody, mailFrom, List))
            {
                //发送成功，进行相应处理
                Response.Write("发送成功");
            }
            else
            {
                //发送失败，进行相应处理
                Response.Write("发送失败");
            }
        }

        /*
         Golf_Address---
         Id,UserId,FristName,LastName,ConsigneeFrist,ConsigneeLast,Mobile,Tel,Fax,PostCode,Address,Email,FitDeliveryTime,Province,City,Borough,CreateTime,IsShow
         
         * 
         * [Id],[OrderNumber],[OrderSumPrice],[UserId],[FristName],[LastName],[ProductSumPrice],[TransportationPrice],[MailTypeId],[IsCoupon],[CouponPrice],[IsAddPrice],[AddPriceState],[AddPrice],[ProductNumberList],[OrderGeneratedTime],[OrderVerifyTime],[VerifyNameId],[AddressId],[UserMessage],[UserIp],[IsPayment],[PaymentTypeId],[PaymentBankId],[PaymentPrice],[PaymentTime],[PaymentOrderNumber],[RefundTypeId],[LogisticsStateId],[Logisticsnumber],[PenskeLogistics],[LogisticsRemarks],[OrderStateCodeId],[ProductOrderNumber],[IsDeliver],[DeliverTime],[DeliverUnitId],[IsPrintDeliver],[AllocateUnitId],[AllocateTime],[AdminMessageId],[IsShow] from [Golf_Order]
         * 
         * select [Id],[OrderId],[OrderNumber],[UserId],[FristName],[LastName],[ProductTypeId],[ProductId],[ProductName],[GiftId],[GiftNumber],[ProductPrice],[BuyNumber],[Integral],[Pro_Specification],[AdminId],[AddTime] from [Golf_OrderProduct]
         * 
         * select [Id],[OrderStateName],[OrderStateCodeRemarks],[AdminId],[AddTime] from [Golf_OrderStateCode]
         */

    }
}

